package com.ipet.sso.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ipet.common.vo.SysResult;
import com.ipet.sso.pojo.User;
import com.ipet.sso.service.UserService;

import redis.clients.jedis.JedisCluster;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private JedisCluster jedisCluster;
	
	@RequestMapping("/check/{param}/{type}")
	public Object checkUser(@PathVariable String param,@PathVariable Integer type,String callback){
		try {
			//根据传递的参数 判断数据是否存在
			Boolean result = userService.findCheckUser(param,type);
			//返回JSONP的数据
			MappingJacksonValue jacksonValue = new MappingJacksonValue(SysResult.oK(result));
			jacksonValue.setJsonpFunction(callback);
			
			return jacksonValue;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 * 注册方法
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(@RequestBody User user){
		
		return userService.saveUser(user);

	}
	
	/**
	 * 登录方法
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String doLogin(@RequestParam("username") String username,
			@RequestParam("password") String password){
		
	
			 return userService.findUserByUP(username,password);
	

		
	}
	
	/**
	 * 根据ticket查询用户的JSON信息
	 */
	@RequestMapping("/query/{ticket}")
	public Object findUserTicket(@PathVariable String ticket, String callback){
		//获取用户的数据
		String userJSON = jedisCluster.get(ticket);
		MappingJacksonValue jacksonValue = new MappingJacksonValue(SysResult.oK(userJSON));
		jacksonValue.setJsonpFunction(callback);
		return jacksonValue;
	}
	
}
